'use strict';
var _ = require('underscore');
module.exports = function (sequelize, DataTypes) {

  var User = sequelize.define('User', {
    id: {
      type: DataTypes.INTEGER.UNSIGNED,
      primaryKey: true,
      autoIncrement: true
    },
    uid: {
      type: DataTypes.STRING(41),
      allowNull: false,
      comment: '用户登录ID'
    },
    region: {
      type: DataTypes.STRING(5),
      allowNull: false,
      validate: {
        isInt: true
      },
      defaultValue: '86',
      comment: '区域默认86'
    },
    contactNumber: {
      type: DataTypes.STRING(16),
      allowNull: true,
      defaultValue: '',
      comment: '联系方式'
    },
    nickname: {
      type: DataTypes.STRING(41),
      allowNull: false,
      comment: '用户昵称'
    },
    portraitUri: {
      type: DataTypes.STRING(256),
      allowNull: false,
      defaultValue: '',
      comment: '用户头像'
    },
    partyGroup: {
      type: DataTypes.STRING(256),
      allowNull: false,
      defaultValue: ''
    },
    storeName: {
      type: DataTypes.STRING(41),
      allowNull: false,
      defaultValue: '',
      comment: '用户对应店铺名称'
    },
    storeId: {
      type: DataTypes.STRING(41),
      allowNull: false,
      defaultValue: '',
      comment: '用户对应店铺ID'
    },
    position: {
      type: DataTypes.STRING(41),
      allowNull: false,
      defaultValue: ''
    },
    rongCloudToken: {
      type: DataTypes.STRING(256),
      allowNull: false,
      defaultValue: ''
    },
    groupCount: {
      type: DataTypes.INTEGER.UNSIGNED,
      allowNull: false,
      defaultValue: 0
    },
    timestamp: {
      type: DataTypes.BIGINT,
      allowNull: false,
      defaultValue: 0,
      comment: '时间戳（版本号）'
    }
  }, {
    classMethods: {
      associate: function (models) {
        User.belongsToMany(models.Team, {
          through: models.TeamUser,
          foreignKey: 'userId',
          constraints: false
        });
      },
      getNicknames: function (userIds) {
        return User.findAll({
          where: {
            id: {
              $in: userIds
            }
          },
          attributes: ['id', 'nickname']
        }).then(function (users) {
          return userIds.map(function (userId) {
            return _.find(users, function (user) {
              return user.id === userId;
            }).nickname;
          });
        });
      },
      getUserBrief: function (userId) {
        return User.findById(userId, {
          attributes: ['nickname', 'uid']
        }).then(function (user) {
          if (user) {
            return user;
          } else {
            return null;
          }
        });
      },
      checkUserExists: function (userId) {
        return User.count({
          where: {
            id: userId
          }
        }).then(function (count) {
          return Promise.resolve(count === 1);
        }).catch(function (err) {
          return Promise.reject(err);
        });
      },
      checkPhoneAvailable: function (region, phone) {
        return User.count({
          where: {
            region: region,
            phone: phone
          }
        }).then(function (count) {
          return Promise.resolve(count === 0);
        }).catch(function (err) {
          return Promise.reject(err);
        });
      }
    },
    paranoid: true,
    indexes: [
      {
        unique: true,
        fields: ['uid']
      }
    ],
    tableName: 'users'
  });


  return User;
};
